﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：
           // 文件功能描述：
           //
           // 
           // 创建标识：
           //
           // 修改标识：
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace RuleCode.ObjectTree
{
    public partial class TableFieldsContextMenu : NewNodeContextMenu
    {
        private ToolStripMenuItem stringToolStripMenuItem;
        private ToolStripMenuItem integerToolStripMenuItem;
        private ToolStripMenuItem realToolStripMenuItem;
        private ToolStripMenuItem dateToolStripMenuItem;
        private ToolStripMenuItem timeToolStripMenuItem;
        private ToolStripMenuItem enumToolStripMenuItem;
        private ToolStripMenuItem containerToolStripMenuItem;
        private ToolStripMenuItem int64ToolStripMenuItem;


        public TableFieldsContextMenu()
        {
            InitializeComponent();
            CustInitillize();
            newNodeToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[]
                                                                {
                                                                    stringToolStripMenuItem, integerToolStripMenuItem,
                                                                    realToolStripMenuItem, dateToolStripMenuItem,
                                                                    timeToolStripMenuItem, enumToolStripMenuItem,
                                                                    containerToolStripMenuItem, int64ToolStripMenuItem
                                                                });
        }

        private void CustInitillize()
        {
            stringToolStripMenuItem = new ToolStripMenuItem();
            integerToolStripMenuItem = new ToolStripMenuItem();
            realToolStripMenuItem = new ToolStripMenuItem();
            dateToolStripMenuItem = new ToolStripMenuItem();
            timeToolStripMenuItem = new ToolStripMenuItem();
            enumToolStripMenuItem = new ToolStripMenuItem();
            containerToolStripMenuItem = new ToolStripMenuItem();
            int64ToolStripMenuItem = new ToolStripMenuItem();
            // 
            // stringToolStripMenuItem
            // 
            stringToolStripMenuItem.Name = "stringToolStripMenuItem";
            stringToolStripMenuItem.Size = new Size(152, 22);
            stringToolStripMenuItem.Text = "String";
            stringToolStripMenuItem.Click += stringToolStripMenuItem_Click;
            // 
            // integerToolStripMenuItem
            // 
            integerToolStripMenuItem.Name = "integerToolStripMenuItem";
            integerToolStripMenuItem.Size = new Size(152, 22);
            integerToolStripMenuItem.Text = "Integer";
            integerToolStripMenuItem.Click += integerToolStripMenuItem_Click;
            // 
            // realToolStripMenuItem
            // 
            realToolStripMenuItem.Name = "realToolStripMenuItem";
            realToolStripMenuItem.Size = new Size(152, 22);
            realToolStripMenuItem.Text = "Real";
            realToolStripMenuItem.Click += realToolStripMenuItem_Click;
            // 
            // dateToolStripMenuItem
            // 
            dateToolStripMenuItem.Name = "dateToolStripMenuItem";
            dateToolStripMenuItem.Size = new Size(152, 22);
            dateToolStripMenuItem.Text = "Date";
            dateToolStripMenuItem.Click += dateToolStripMenuItem_Click;
            // 
            // timeToolStripMenuItem
            // 
            timeToolStripMenuItem.Name = "timeToolStripMenuItem";
            timeToolStripMenuItem.Size = new Size(152, 22);
            timeToolStripMenuItem.Text = "Time";
            timeToolStripMenuItem.Click += timeToolStripMenuItem_Click;
            // 
            // enumToolStripMenuItem
            // 
            enumToolStripMenuItem.Name = "enumToolStripMenuItem";
            enumToolStripMenuItem.Size = new Size(152, 22);
            enumToolStripMenuItem.Text = "Enum";
            enumToolStripMenuItem.Click += enumToolStripMenuItem_Click;
            // 
            // containerToolStripMenuItem
            // 
            containerToolStripMenuItem.Name = "containerToolStripMenuItem";
            containerToolStripMenuItem.Size = new Size(152, 22);
            containerToolStripMenuItem.Text = "Container";
            containerToolStripMenuItem.Click += containerToolStripMenuItem_Click;

            //
            int64ToolStripMenuItem.Name = "int64ToolStripMenuItem";
            int64ToolStripMenuItem.Size = new Size(152, 22);
            int64ToolStripMenuItem.Text = "Int64";
            int64ToolStripMenuItem.Click += int64ToolStripMenuItem_Click;
        }

        private void int64ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldInt64Property), EDTTypes.EDTTypeInt64);
        }

        private void containerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldContainerProperty), EDTTypes.EDTtypeContainer);
        }

        private void enumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldEnumProperty), EDTTypes.EDTTypeEnum);
        }

        private void timeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldTimeProperty), EDTTypes.EDTTypeTime);
        }

        private void dateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldDateProperty), EDTTypes.EDTTypeDate);
        }

        private void realToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldRealProperty), EDTTypes.EDTTypeReal);
        }

        private void integerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldIntProperty), EDTTypes.EDTTypeInt);
        }

        private void stringToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addField(typeof(TableFieldStringProperty), EDTTypes.EDTTypeString);
        }

        public void addField(Type type, string edtType)
        {
            string name = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.TableFields, OT.Field, type, "");
            SqlHelper helper = SqlHelper.Instance;
            helper.ExecuteNonQuery(CommandType.Text,
                                   "alter table [" + Util.SelectedNode.SysXmlNode.ParentNode.ParentNode.Name + "] add [" +
                                   name + "] " + Util.getTableFieldFormat(edtType, null));
        }
    }
}